<?php

namespace App\Http\Controllers;

use App\Models\Category;
use Illuminate\Http\Request;
use App\Models\Product;

class NewProductsController extends ApiBaseController
{
    public function categories()
    {
        $category = new Category();
        return $this->success($category->getCategoryTree());
    }

    public function products(Request $request)
    {
        // 创建一个查询构造器
        $builder = Product::query()->where('on_sale', true);

        if ($category_id = $request->input('category_id', '')) {
            $builder->where('category_id', $category_id);
        }
        // 判断是否有提交 search 参数，如果有就赋值给 $search 变量
        // search 参数用来模糊搜索商品
        if ($search = $request->input('search', '')) {
            $like = '%' . $search . '%';
            // 模糊搜索商品标题、商品详情、SKU 标题、SKU描述
            $builder->where(function ($query) use ($like) {
                $query->where('title', 'like', $like)
                    ->orWhere('description', 'like', $like)
                    ->orWhereHas('skus', function ($query) use ($like) {
                        $query->where('title', 'like', $like)
                            ->orWhere('description', 'like', $like);
                    });
            });
        }

        // 是否有提交 order 参数，如果有就赋值给 $order 变量
        // order 参数用来控制商品的排序规则
        if ($order = $request->input('order', '')) {
            // 是否是以 _asc 或者 _desc 结尾
            if (preg_match('/^(.+)_(asc|desc)$/', $order, $m)) {
                // 如果字符串的开头是这 3 个字符串之一，说明是一个合法的排序值
                if (in_array($m[1], ['price', 'sold_count', 'rating'])) {
                    // 根据传入的排序值来构造排序参数
                    $builder->orderBy($m[1], $m[2]);
                }
            }
        }

        $perPage = $request->input('per_page', 10);
        $page = $request->input('page', 1);

        $products = $builder->paginate($perPage, '*', 'page', $page);
        foreach ($products->items() as $product) {
            $product->image = $product->image ? \Storage::disk('public')->url($product->image) : '';
            if ($product->description_image) {
                $descriptionImages = [];
                foreach ($product->description_image as $key => $value) {
                    $descriptionImages[$key] = \Storage::disk('public')->url($value);
                }
                // 最后将处理后的数组重新赋值给 description_image 属性
                $product->description_image = $descriptionImages;
            }
        }

        return $this->success([
            'products' => $products,
            'filters' => [
                'search' => $search,
                'order' => $order,
            ],
        ]);
    }


    public function productDetail(Request $request)
    {
        $id = $request->input('id');

        $product = Product::query()->where('id', $id)->first();
        if (!$product) {
            return $this->failure('500', '商品不存在');
        }

        $product->image = $product->image ? \Storage::disk('public')->url($product->image) : '';
        if ($product->description_image) {
            $descriptionImages = [];
            foreach ($product->description_image as $key => $value) {
                $descriptionImages[$key] = \Storage::disk('public')->url($value);
            }
            // 最后将处理后的数组重新赋值给 description_image 属性
            $product->description_image = $descriptionImages;
        }
        return $this->success($product);
    }
}
